//
//  StatusCellPictureView.swift
//  WeiBo_One
//
//  Created by apple on 15/10/28.
//  Copyright © 2015年 蜡笔小炎. All rights reserved.
//

import UIKit


class StatusCellPictureView: UICollectionView {
    
    /// 微博配图视图 Cell 可重用标识符
    private let StatusCellPictureViewCellId = "StatusCellPictureViewCellId"
    // 配图视图格子间距
    private let statusCellPictureItemMargin: CGFloat = 8
    // 微博视图模型，图片视图的数据入口
    var statusViewModel: StatusViewModel? {
        didSet {
            sizeToFit()
            
            reloadData()
        }
    }

    override func sizeThatFits(size: CGSize) -> CGSize {
        
        let s = pictureViewSize()
        return s
    }
    
    // MARK: - 构造函数
    init() {
        // 创建布局
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = statusCellPictureItemMargin
        layout.minimumLineSpacing = statusCellPictureItemMargin
        layout.itemSize = CGSize(width: 20, height: 20)
        //初始化
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        //背景色
        backgroundColor = UIColor.redColor()
        //注册cell
        registerClass(StatusCellPictureViewCell.self, forCellWithReuseIdentifier: StatusCellPictureViewCellId)
        //数据源
        dataSource = self
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 实现数据源方法
extension StatusCellPictureView: UICollectionViewDataSource {
    //返回cell个数
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(statusViewModel?.thumvnailUrls?.count ?? 0)
        return statusViewModel?.thumvnailUrls?.count ?? 0
    }
    //放回cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StatusCellPictureViewCellId, forIndexPath: indexPath) as! StatusCellPictureViewCell
        
        cell.backgroundColor = UIColor.blackColor()
        
        print(cell.self)
        
        return cell
    }
}

// 计算配图视图尺寸
extension StatusCellPictureView {
    // 返回 配图视图 的尺寸
    private func pictureViewSize() -> CGSize {
        
        let rowCount: CGFloat = 3
        let count = statusViewModel?.status.pic_urls?.count ?? 0
        
        let maxWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellMargin
        let itemWidth = (maxWidth - 2 * statusCellPictureItemMargin) / rowCount
        // 设置配图布局中 itemSize 大小
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
   
        if count == 0 {
            return CGSizeZero
        }
        if count == 1 {
            //TODO: - 临时数据，再改
            return CGSizeMake(150, 80)
        }
        if count == 4 {
            let w = 2 * itemWidth + statusCellPictureItemMargin
            return CGSizeMake(w, w)
        }
        
        let row =  CGFloat(count - 1) / rowCount + 1.0
        let w = 3 * itemWidth + 2 * statusCellPictureItemMargin
        let h = row * itemWidth + (row - 1) * statusCellPictureItemMargin
        
        return CGSizeMake(w, h)
    }
}


// MARK: - 私有Cell类，图片collectionViewCell
extension StatusCellPictureView {

    private class StatusCellPictureViewCell: UICollectionViewCell {
        
        
    }
}








